﻿using System.IO;
using System.Text;
using System.Transactions;
using System.Xml.Serialization;
using CNav.Business;
using CNav.Business.Validation;
using CNav.Common;
using CNav.Common.Support;
using CNav.DataEntity;
using CNav.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using CNav.Domain.Admin;
using CNav.Domain.CaseWork;

namespace CNav.Business.Services
{
    public interface IEnquiryService
    {
        ResponseData<int> AddEnquiry(NewEnquiry enquiry, List<RenderControl> customForm);
    }

    public class EnquiryService : ServiceBase, IEnquiryService
    {
        public ResponseData<int> AddEnquiry(NewEnquiry enquiry, List<RenderControl> customForm)
        {
            using (TransactionScope scop = new TransactionScope())
            {
                var enquiryDto = new EnquiryDto();
                enquiryDto.BusinessID = enquiry.BusinessId;
                enquiryDto.ComplaintTypeID = enquiry.ComplaintType;
                enquiryDto.ConsumerID = enquiry.ConsumerId;
                enquiryDto.CreatedOnUtc = DateTime.UtcNow;
                enquiryDto.Description = enquiry.Description;
                enquiryDto.OwnerUserID = enquiry.UserId;
                enquiryDto.ProductID = enquiry.ProductId;

                Context.Repository<EnquiryDto>().Add(enquiryDto);

                var enquiryAnswer = new EnquiryCustomFormAnswerDto();
                enquiryAnswer.EnquiryID = enquiryDto.ID;
                enquiryAnswer.FormID = enquiry.FormId;
                enquiryAnswer.AnswerXml = XmlUtil.Serialize(customForm);

                Context.Repository<EnquiryCustomFormAnswerDto>().Add(enquiryAnswer);

                Context.Commit();
                scop.Complete();

                return new ResponseData<int> { Data = enquiryDto.ID };
            }
        }

        public ResponseData<Product> UpdateEnquiry(Product product)
        {
            throw new NotImplementedException();
        }

        public ResponseData<bool> DeleteEnquiry(int id)
        {
            throw new NotImplementedException();
        }

        public ResponseData<List<Product>> GetEnquiryList()
        {
            throw new NotImplementedException();
        }

        public ResponseData<Product> GetEnquiry(int id)
        {
            throw new NotImplementedException();
        }
    }
}